<!DOCTYPE html>
<html>
<head>
<head />
<div th:include="base-libs :: base-libs" />
<title>解约</title>
<style type="text/css">
.topBtn{
	float: right;
	margin-right:10px;
	margin-top:5px
}
</style>
</head>
<script th:inline="javascript">
	/**签约申请医联体信息*/
	var qysqxx = [[${qysqxx}]];
	/**签约机构信息*/
	var qyjgxx = [[${qyjgxx}]];
</script>
<script type="text/javascript">
	$(function() {
		$(this).registerManagerPage();
		initView();
		initData(); 
	});

	function initData() {
		/**加载医联体信息*/
		initQysqxx();
		/**加载签约机构信息*/
		initQyjgxx();
	}

	function initView() {
		$("#jgxxTable").datagrid(getOption());
		var info;
		$(qyjgxx).each(function(index,item){
			if(item["JGLX"]=="3"){
				info = item;
			}
		});
		var sj = getAdd(info["QYSJ"]);
		var start = {
				elem: "#XYQYSJ",
				min: sj,
				choose: function(datas){
					getDate(datas);
			}};
		laydate(start);
		$("#XYQYYXQ").trigger("change");
		
		/**判断有效期是否选择*/
		$("#XYQYYXQ").on("change", function(){
			var data = $("#XYQYSJ").val();
			if(data) getDate(data);
		});
		
		/**续约按钮*/
		$("#xyBtn").on("click", function(){
			if($(".form-control").QuickValidate() && validateDqsj()){
				var params = {};
				/**获取机构IDs*/
				var info = $("#jgxxTable").datagrid("getData");
				var rows = info["rows"];
				var yljgids = "";
				if(qysqxx["YLTLX"] == "03"){
					var ksdms = "";
					for(var i in rows){
						yljgids += rows[i]["PTJGID"] + ",";
						ksdms += rows[i]["PTJGID"] + ":" + rows[i]["KSDM"] + "/";
					}
					params["KSDMS"] = ksdms.substr(0, ksdms.length-1);
				}else{
					for(var i in rows){
						yljgids += rows[i]["PTJGID"] + ",";
					}
				}
				yljgids = yljgids.substr(0, yljgids.length -1);
				params["YLJGIDS"] = yljgids;
				params["ZDYLJGID"] = qysqxx["ZDYLJGID"];
				params["YLTID"] = qysqxx["YLTID"];
				params["QYYXQ"] = $("#XYQYYXQ").val();
				params["QYSJ"] = $("#XYQYSJ").val();
				params["DQSJ"] = $("#XYDQSJ").val();
				params["method"] = "xy";
				$.QuickRemote.AjaxFiledFormSubmit("managerForm", function(result) { 
					var info = result["info"];
					if (result["state"]) {
						$.QuickAlert.alertNormal("续约成功！",{callback: function(){
							$.QuickUrlUtils.redirectUrl("index.html");
						}});
					} else {
						$.QuickAlert.alertFail({
							content : info["faultInfo"]
						});
					}
				}, params);
			}
		});
	}

	/**返回按钮*/
	function btnBackHandler(){
		$.QuickUrlUtils.redirectUrl("index.html");
	}
	
	/**加载医联体信息*/
	function initQysqxx(){
		if(qysqxx){
			var info;
			$(qyjgxx).each(function(index,item){
				if(item["JGLX"]=="3"){
					info = item;
				}
			});
			info = $.extend({}, qysqxx, info);
			$(".yltxx").find(".dataField").each(function(){
				$(this).text(info[$(this).attr("id")]);
			});
			$("#BZSM").text(qysqxx["BZSM"]);
		}
		$("#YLTID").val(qysqxx["YLTID"]);
	}
	
	/**加载签约机构信息*/
	function initQyjgxx(){
		if(qyjgxx){
			if(qysqxx["YLTLX"] == "03"){
				$("#jgxxTable").datagrid(getOption(qysqxx["YLTLX"]));
			}
			var length = 37*(qyjgxx.length);
			$("#maxDiv").css("height",length+560+"px");
			$("#maxLayout").layout("resize");
			$("#jgxxBody").css("height", length + 43+"px");
			$("#jgxxTable").datagrid("loadData",{"total": qyjgxx.length, "rows": qyjgxx});
		}
	}
	
	/**获取option*/
	function getOption(lx){
		var opt;
		if(lx == "03"){
			opt= {
					idField : "YLJGDM",
					uniqueId: "PTJGID",
					striped : true,
					border:false,
					clickToSelect: true,
					fit: true,
					fitColumns: true,
					singleSelect: false,
					rownumbers: true,
					columns : [[
						{
							field : "YLJGMC",
							width: 160,
							align: "center",
							title : "机构名称"
						},
						{
							field : "XZQHMC",
							width: 100,
							align: "center",
							title : "所属区域"
						},
						{
							field : "WSJGLBMC",
							width: 120,
							align: "center",
							title : "机构类型"
						},
						{
							field : "KSMC",
							width: 300,
							align: "center",
							title : "科室",
							formatter : function(value, row, index){
								return "<span title='"+value+"'>"+value+"</span>";
							}
						},
						{
							field : "YYDJMC",
							width: 100,
							align: "center",
							title : "医院等级"
						},
						{
							field : "JGFLGLMC",
							width: 120,
							align: "center",
							title : "经营性质"
						},
						{
							field : "cz",
							width: 100,
							align: "center",
							title : "操作",
							formatter : function(value, row, index){
								if(row["PTJGID"] != qysqxx["ZDYLJGID"]){
									value = $("#tableToolBar").html()
									return value.replace(/#index#/g,index);
								}
							}
						}
					]],onLoadSuccess: function(data){
						$("#jgxxLayout").layout("resize");
					}
				};
		}
		else{
			opt= {
					idField : "YLJGDM",
					uniqueId: "PTJGID",
					striped : true,
					border:false,
					clickToSelect: true,
					fit: true,
					fitColumns: true,
					singleSelect: false,
					rownumbers: true,
					columns : [[
						{
							field : "YLJGMC",
							width: 250,
							align: "center",
							title : "机构名称"
						},
						{
							field : "XZQHMC",
							width: 150,
							align: "center",
							title : "所属区域"
						},
						{
							field : "WSJGLBMC",
							width: 150,
							align: "center",
							title : "机构类型"
						},
						{
							field : "YYDJMC",
							width: 150,
							align: "center",
							title : "医院等级"
						},
						{
							field : "JGFLGLMC",
							width: 150,
							align: "center",
							title : "经营性质"
						},
						{
							field : "cz",
							width: 100,
							align: "center",
							title : "操作",
							formatter : function(value, row, index){
								if(row["PTJGID"] != qysqxx["ZDYLJGID"]){
									value = $("#tableToolBar").html()
									return value.replace(/#index#/g,index);
								}
							}
						}
					]],onLoadSuccess: function(data){
						$("#jgxxLayout").layout("resize");
					}
				};
		}
		return opt;
	}
	
	/**生成到期时间*/
	function getDate(datas){
		datas = datas.split("-");
		var lx = $("#XYQYYXQ").val();
    	if(lx == "01"){
    		datas = new Date(datas[0], parseInt(datas[1]) -1 + 1, datas[2]); 
    	}
    	else if(lx == "02"){
    		datas = new Date(datas[0], parseInt(datas[1]) -1 + 3, datas[2]); 
    	}
		else if(lx == "03"){
			datas = new Date(datas[0], parseInt(datas[1]) -1 + 6, datas[2]); 
    	}
		else if(lx == "04"){
			datas = new Date(parseInt(datas[0]) + 1, datas[1], datas[2]); 
		}
    	datas = datas.getFullYear()+"-"+(parseInt(datas.getMonth()) +1) +"-"+datas.getDate();
    	$("#XYDQSJ").val(datas);
	}
	
	function getAdd(datas){
		datas = datas.split("-");
		datas = new Date(datas[0], datas[1] -1, parseInt(datas[2])+ 1); 
		return datas.getFullYear()+"-"+(parseInt(datas.getMonth()) +1) +"-"+datas.getDate();
	}
	
	/**校验到期时间*/
	function validateDqsj(){
		var flag = true;
		var data1 = $("#DQSJ").text();
		data1 = new Date(data1.replace(/\-/g, "\/")); 
		var data2 = $("#XYDQSJ").val();
		data2 = new Date(data2.replace(/\-/g, "\/")); 
		if(data1 >= data2){
			flag = false;
			$.QuickAlert.alertFail({
				content : "续约到期时间必须大于签约到期时间！",
			});
		}
		return flag;
	}
	
	/**移除医疗机构*/
	function remove(index){
		$("#jgxxTable").datagrid("deleteRow",index);
		var data = $("#jgxxTable").datagrid("getData");
		/**清除所有数据*/
		$("#jgxxTable").datagrid("loadData", { total: 0, rows: [] }); 
		/**重新加载数据*/
		$("#jgxxTable").datagrid("loadData",data);
		data = $("#jgxxTable").datagrid("getData");
	}
</script>

<body style="border: none; overflow-y: scroll;">
<div id="maxDiv" style="height: 560px;">
	<div  id="maxLayout" class="easyui-layout" style="border: none; overflow: hidden;">
		<div region="north" style="border: none; overflow: hidden;">
			<h4 class="h4-title" style="font-size: 18px;">
				续约
			<!-- 	<button class="btn topBtn" style="float:right" type="button" onclick="btnBackHandler()">返回</button>
				<button class="btn topBtn" style="float:right" type="button" id="xyBtn">续约</button>
			 --></h4>
			<div class="form-table-box" >
				<form id="managerForm" class="form-horizontal" action="save"></form>
					<h4 class="h4-title" style="font-size: 13px;">
						医联体信息
					</h4>
					<table class="form-table yltxx">
						<tr>
							<td class="td-text">医联体名称：</td>
							<td class="td-content">
								<span id="YLTMC" class="dataField"></span>
								<input style="display:none;" id="YLTID">
							</td>
							<td class="td-text">医联体类型：</td>
							<td class="td-content">
								<span id="YLTLXMC" class="dataField"></span>
							</td>
							<td class="td-text">主导医疗机构：</td>
							<td class="td-content">
								<span id="ZDYLJGMC" class="dataField"></span>
							</td>
						</tr>
						<tr>
							<td class="td-text">签约有效期：</td>
							<td class="td-content">
								<span id="QYYXQMC" class="dataField"></span>
							</td>
							<td class="td-text">签约时间：</td>
							<td class="td-content">
								<span id="QYSJ" class="dataField"></span>
							</td>
							<td class="td-text">至</td>
							<td class="td-content">
								<span id="DQSJ" class="dataField">
								</span>
							</td>
						</tr>
					</table>
			</div>
		</div>
		<div region="center" style="border: none;overflow: hidden;">
			<h4 class="h4-title" style="font-size: 13px;">
				添加机构
			</h4>
			<div  id="jgxxBody" style="height:120px;width:96.5%;margin-left:2.5%;">
				<div  id="jgxxLayout" class="easyui-layout" fit="true">
					<div region="center" style="border: none;">
						<table id="jgxxTable" >
									</table>
					</div>
				</div>
			</div>
			<div style="height: 320px;">
				<div class="easyui-layout" fit="true">
					<div region="center" style="border: none; overflow: hidden;">
						<div class="form-table-box" >
							<table class="form-table">
								<tr>
									<td class="td-text">备注说明：</td>
									<td class="td-content" colspan="5">
										<span id="BZSM"></span>
									</td>
								</tr>
							</table>
							<h4 class="h4-title" style="font-size: 13px;">
								续约信息
							</h4>
							<table class="form-table fqjg">
								<tr>
									<td class="td-text">签约有效期：</td>
									<td class="td-content">
										<select id="XYQYYXQ" name="XYQYYXQ" class="form-control dataField cacheTable yltxx" style="width:90%;"
										cacheTable="TB_ZY_YXQ" quick-config="label:'签约有效期', validateProxy:'notNull'">
										</select>
									</td>
									<td class="td-text">签约时间：</td>
									<td class="td-content">
										<input id="XYQYSJ" name="XYQYSJ" style="height:30px; width: 80%;" readonly="readonly"
										class="form-control input-text laydate-icon dataField yltxx" quick-config="label:'签约时间', validateProxy:'notNull'" />
									
									</td>
									<td class="td-text">至</td>
									<td class="td-content">
										<input id="XYDQSJ" name="XYDQSJ" readonly="readonly"
										class="form-control dataField yltxx" quick-config="label:'到期时间', validateProxy:'notNull'" /><!-- onclick="laydate(end)" -->
									</td> 
								</tr>
							</table>
							<div class="form-table-box" >
								<h4 class="h4-title" style="font-size: 13px;">
									服务协议信息
								</h4>
								<table class="form-table">
									<tr>
										<td class="td-content">
											<input class="easyui-textbox" readonly="readonly" data-options="multiline:true" style="width:100%;height:100px">
										</td>
									</tr>
								</table>
							</div>
							<div style="text-align: center;padding-bottom:10px;">
								<button class="btn" type="button" id="xyBtn">续约</button>
								&nbsp;&nbsp;
								<button class="btn" type="button" onclick="btnBackHandler()">返回</button>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</body>
<script type="text/html" id="tableToolBar">
<a class="tableToolBarBtn" href="javascript:remove(#index#)" style="" title="移除" >移除</a>
</script>
</html>


